// pages/participant/index.js
import {
  getParticipantInfo,
  selectParticipantInfo
} from '../../api/information.js'
import { removeObj } from '../../utils/removeObj.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedUser: [], //存放已经选择的参赛人员列表
    skuId: '', // 参赛类别ID
    count: 0, //决定按钮的颜色
    submitstate: false, //是否选好了
    obtainList: [], //满足参赛人员
    isRequst: false //用来判断当前是否为请求状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      skuId: options.skuId,
      selectedUser:options.selecteduser?JSON.parse(options.selecteduser):options.selecteduser
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.feach()
  },
  feach() {
    this.setData({isRequst: false})
    this._selectParticipantInfo()
  },
  //获取列表
  _selectParticipantInfo() {
    if (!this.data.isRequst) {
      this.setData({isRequst: true})
      selectParticipantInfo(this.data.skuId).then(res => {
        //满足参赛资质
        let arr = removeObj(res.data.obtainList, ['searchValue', 'createBy', 'createTime', 'remark', 'updateBy', 'updateTime', 'params', 'nickname', 'countryId', 'provinceId', 'cityId']);
        // if (arr.length > 0) {
          arr.forEach(e => {
            e.state = false
            e.shortAddress1 = e.shortAddress
            e.shortAddress = e.shortAddress.split(',')[0] + e.shortAddress.split(',')[1]
          })
          let users = this.data.selectedUser
          if (users) {
            arr.forEach(item => {
              users.forEach((sonitem, i) => {
                //恢复已勾选的状态
                if (sonitem.participantId == item.participantId) {
                  item.state = true
                }
              });
            });
          }
          this.setData({
            obtainList: arr
          })
        // }
        this.setData({isRequst: false})
        this.buttoncolorfun()
      })
    }
  },
  goto(e) {
    let index = e.currentTarget.dataset.index
    let u = this.data.obtainList[index]
    wx.navigateTo({
      url: `/pages/input_information/index?stop=true&pid=${u.participantId}&uid=${u.userId}&qualificateId=${JSON.parse(wx.getStorageSync('data')).qualificateList[0].qualificateId}`
    })
  },
  //修改
  update(e) {
    wx.navigateTo({
      url: '/pages/entry_update/index?user=' + JSON.stringify(e.currentTarget.dataset.item),
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this._getParticipantInfo()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.feach()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  choosestate(e) {
    let d = e.currentTarget.dataset
    if (d.enrolled == 0) {
      this.setData({
        ['obtainList.[' + d.index + '].state']: d.state ? false : true
      })
      this.buttoncolorfun()
    }else{
      wx.showToast({title: '参赛人员或已报名该赛事',icon:'none'})
    }
  },
  //按钮颜色
  buttoncolorfun() {
    let count = 0
    let that = this
    let user = []
    this.data.obtainList.forEach((item, e) => {
      if (item.state == 1) {
        count++
        user.push(item)
      }
    });
    this.setData({
      count: count,
      selectedUser: user
    })
  },
  //跳转到添加参赛人员的页面
  add() {
    wx.navigateTo({
      url: '/pages/entry_add/index',
    })
  },
  //选好了
  choose() {
    if (!this.data.isRequst) {
      let { count, selectedUser } = this.data // 当前页面的数据
      if (count > 0) {
        let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
        let prevPage = pages[pages.length - 2];  //上个页面
        let { select, matchSkuList } = prevPage.data // 上个页面的数据
        //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
        let oldUser = matchSkuList[select].selectedUser;
        if (oldUser) {//已经存过用户数据的了
          let newArr = []
          oldUser.forEach(e => {
            const index = selectedUser.findIndex(newUser => newUser.participantId === e.participantId);//匹配下标
            if (index != -1) {//匹配成功时
              selectedUser[index].hasSkuQualificate = e.hasSkuQualificate
              if (e.matchFormList) {
                selectedUser[index].matchFormList = e.matchFormList
              }
              if (e.arr) {
                selectedUser[index].arr = e.arr
              }
            }
          })
        }
        prevPage.setData({ ['matchSkuList[' + select + '].selectedUser']: selectedUser })
        wx.navigateBack(1)
      } else {
        wx.showToast({
          title: '请选择参赛人员',
        })
      }
    }
  }
})